package com.example.zhuma;

import androidx.appcompat.app.AppCompatActivity;


import android.media.MediaDataSource;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.VideoView;

import com.example.zhuma.entity.Message;
import com.example.zhuma.entity.MessageType;
import com.example.zhuma.utill.ClientSocket;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MonitorActivity extends AppCompatActivity {
    private ClientSocket cs;
    public static VideoView videoView;
    @Override
    /**
     * 视频模块，进入视频模块进行如下操作：
     * 1.向服务器发送请求，获取桌宠眼部视频
     * 2.桌宠不断向服务器发送视频
     * 3.服务器接收视频向手机传输
     */

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor);
        Message message =new Message();
        message.setType(MessageType.MQ_VIDEO);
        Gson gson = new Gson();
        String jsonString = gson.toJson(message);
        try {
            cs= new ClientSocket();
            cs.sendMessage(jsonString);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //去除上方导航框
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }
    public static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            String data = (String)msg.obj;
            System.out.println("最终获得数据"+data);//将数据添加进去
            Gson gson = new Gson();
            Message ms = gson.fromJson(data, Message.class);
            int[][] binaryData = ms.getVideo(); // 你的int类型的二维数组表示的图片

            byte[] byteArray = intArrayToByteArray(binaryData);
            InputStream inputStream = new ByteArrayInputStream(byteArray);
            MediaDataSource mediaDataSource = new MyMediaDataSource(inputStream);
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(mediaDataSource);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    videoView.setVideoURI(Uri.parse("media://" + mediaDataSource));
                    videoView.start();
                }
            });


        }
    };
    private static byte[] intArrayToByteArray(int[][] array) {
        int height = array.length;
        int width = array[0].length;
        byte[] byteArray = new byte[height * width * 4];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int value = array[i][j];
                byteArray[(i * width + j) * 4] = (byte) (value >>> 24);
                byteArray[(i * width + j) * 4 + 1] = (byte) (value >>> 16);
                byteArray[(i * width + j) * 4 + 2] = (byte) (value >>> 8);
                byteArray[(i * width + j) * 4 + 3] = (byte) value;
            }
        }
        return byteArray;
    }
}
class MyMediaDataSource extends MediaDataSource {
    private InputStream inputStream;

    public MyMediaDataSource(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public int readAt(long l, byte[] bytes, int i, int i1) throws IOException {
        return 0;
    }

    @Override
    public long getSize() throws IOException {
        return inputStream.available();
    }

    @Override
    public void close() throws IOException {

    }
}
